package com.waitingresult.demo.aspect;

import com.alibaba.fastjson2.JSON;
import com.waitingresult.demo.annotation.CheckEnumsByInt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: WangShuai
 * @CreateTime: 2023/5/18  12:59
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@Component
public class CheckEnumsByIntValidator implements ConstraintValidator<CheckEnumsByInt,Integer> {

    @Override
    public void initialize(CheckEnumsByInt constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);



        Class aClass = constraintAnnotation.modelTypeEnums();
        Method[] declaredMethods1 = aClass.getDeclaredMethods();
       // System.err.println(Arrays.toString(declaredMethods1));
        try {
            Field[] fields = aClass.getFields();
            for (Field field : fields) {
                System.out.println(field);
            }

            Method values = aClass.getMethod("values");
            Object invoke = values.invoke(aClass);

            String s = JSON.toJSONString(invoke);
            System.out.println(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

//        try {
//            Method values = aClass.getMethod("values()", aClass);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }



        //  Object o = aClass.newInstance();
        //
        Object[] enumConstants = aClass.getEnumConstants();
    //    System.err.println(Arrays.toString(enumConstants));

        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
           // System.err.println(declaredMethod.getName());
        }

       // Object[] enumConstants = aClass.getEnumConstants();
     //   System.err.println(Arrays.toString(enumConstants));
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
       // System.err.println("isvalid:"+value);
        return true;
    }
}
